<template>
  <Layout>
    <div ref="mainContainer" class="main-container" v-loading="isLoading">
      <BlogDetail :blog="data" v-if="data" />
      <BlogComment v-if="!isLoading" />
    </div>
    <template #right>
      <div class="right" v-loading="isLoading">
        <BlogTOC :toc="data.toc" v-if="data" />
      </div>
    </template>
  </Layout>
</template>

<script>
import Layout from "@/components/Layout/index.vue";
import BlogTOC from "./components/BlogTOC.vue";
import BlogDetail from "./components/BlogDetail.vue";
import fetchData from "@/mixin/fetchData.js";
import { getBlog } from "@/api/blog.js";
import BlogComment from "./components/BlogComment";
import mainScroll from "@/mixin/mainScroll.js";
import setTitle from "@/utils/setTitle.js";

export default {
  mixins: [fetchData(null), mainScroll("mainContainer")],
  components: {
    Layout,
    BlogTOC,
    BlogDetail,
    BlogComment,
  },

  methods: {
    async fetchData() {
      var resp = await getBlog(this.$route.params.id);
      resp = null;
      if (!resp) {
        // 没有文章
        this.$router.push("/404");
        return;
      }
      setTitle.setRouterTitle(resp.siteTitle);
      return resp;
    },
    // scroll() {
    //   //为事件总线触发事件
    //   this.$bus.$emit("mainScroll", this.$refs.mainContainer);
    // },
    // scrollTop(top) {
    //   this.$refs.mainContainer.scrollTop = top;
    // },
  },

  //   mounted() {
  //     this.$refs.mainContainer.addEventListener("scroll", this.scroll);
  //     this.$bus.$on("scrollTop", this.scrollTop);
  //   },
  //   beforeDestroy() {
  //     this.$bus.$emit("mainScroll");
  //     this.$refs.mainContainer.removeEventListener("scroll", this.scroll);
  //     this.$bus.$off("scrollTop", this.scrollTop);
  //   },
  updated() {
    var hash = location.hash;
    location.hash = "";
    setTimeout(() => {
      location.hash = hash;
    }, 50);
  },
};
</script>

<style lang="less" scoped>
.main-container {
  position: relative;
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  padding: 20px;
  overflow-y: scroll;
  scroll-behavior: smooth;
}
.right {
  position: relative;
  padding: 20px;
  width: 300px;
  height: 100%;
  overflow-y: scroll;
  box-sizing: border-box;
}
</style>